package com.ordertracker.to;

import java.util.Date;

import com.ordertracker.Session;
import com.ordertracker.constant.Sex;
import com.ordertracker.entity.*;
import com.ordertracker.persistent.constant.DOCUMENT_TYPE;
import com.ordertracker.persistent.constant.DocumentState;
import com.ordertracker.persistent.constant.ROLE_TYPE;
import com.ordertracker.persistent.constant.SEX;
import com.ordertracker.persistent.dictionary.*;
import com.ordertracker.persistent.dictionary.Client;
import com.ordertracker.persistent.dictionary.Employee;
import com.ordertracker.persistent.dictionary.Organization;
import com.ordertracker.persistent.dictionary.Person;
import com.ordertracker.persistent.dictionary.Position;
import com.ordertracker.persistent.dictionary.Role;
import com.ordertracker.persistent.dictionary.User;
import com.ordertracker.persistent.document.PaymentOrder;
import com.ordertracker.util.DateUtils;

/**
 * Description of class.
 * Created 1/18/13 6:23 PM
 *
 * @author andrey.rodin@playtech.com
 */

public final class TransferObjectFactory {
	
	public com.ordertracker.entity.document.PaymentOrder paymentOrderToDTO(
			PaymentOrder paymentOrder) {
		com.ordertracker.entity.Organization org = organizationToDTO(paymentOrder
				.getOrganization());
		com.ordertracker.entity.Client client = clientToDTO(paymentOrder
				.getClient());
		com.ordertracker.entity.document.Currency currency = currencyToDTO(paymentOrder
				.getCurrency());

		com.ordertracker.entity.document.PaymentOrder paymentOrd = new com.ordertracker.entity.document.PaymentOrder(
				paymentOrder.getDate(), null, org, client,
				paymentOrder.getComments(), null, currency,
				paymentOrder.getCurrencyRate(), paymentOrder.getSum());

		return paymentOrd;
	}
	
	public PaymentOrder paymentOrderFromDTO(com.ordertracker.entity.document.PaymentOrder paymentOrder){
		PaymentOrder po = new PaymentOrder();
		//po.setId(paymentOrder.getId());
		po.setClient(clientFromDTO(paymentOrder.getClient()));
		po.setComments(paymentOrder.getComments());
		po.setCurrency(currencyFromDTO(paymentOrder.getCurrency()));
		po.setCurrencyRate(paymentOrder.getCurrencyRate());
		po.setDate(paymentOrder.getDate());
		po.setExternalId(null);
		po.setOrganization(organizationFromDTO(paymentOrder.getOrganization()));
		po.setSum(paymentOrder.getSum());
		po.setDocumentType(DOCUMENT_TYPE.valueOf(paymentOrder.getDocumentType().name()));
		po.setState(DocumentState.SAVED);
		//po.setVersion(paymentOrder.getVersion());
		
		return po;
	}
	
	public com.ordertracker.entity.document.Currency currencyToDTO(Currency currency){
		
		com.ordertracker.entity.document.Currency curr = new com.ordertracker.entity.document.Currency(currency.getCode(), currency.getName(), currency.getShortName());
		
		return curr;
	}	
    public Currency currencyFromDTO(com.ordertracker.entity.document.Currency currencyDTO) {
		if (currencyDTO==null){
			return null;
		}
    	
    	Currency currency = new Currency();
    	currency.setId(currencyDTO.getId());
    	currency.setCode(currencyDTO.getCode());
    	currency.setName(currencyDTO.getName());
    	currency.setShortName(currencyDTO.getShortName());
    	
        return currency;
    }
    
    public com.ordertracker.entity.User userToDTO(User user) {
        com.ordertracker.entity.Person person = personToDTO(user.getPerson());
        com.ordertracker.entity.Role role = roleToDTO(user.getRole());
        com.ordertracker.entity.User usrDto = new com.ordertracker.entity.User(user.getId(), 
                user.getVersion(), user.getName(),user.getLogin(),
                user.getDescription(), user.getEmail(), role, person);
        usrDto.setLocale(user.getLocale());
        usrDto.setTimezone(user.getTimezone());
        return usrDto;
    }

    public User userFromDTO(com.ordertracker.entity.User userDto) {
        User user = new User();
        user.setId(userDto.getId());
        user.setVersion(userDto.getVersion());
        user.setDescription(userDto.getDescription());
        user.setEmail(userDto.getEmail());
        user.setName(userDto.getName());
        user.setLogin(userDto.getLogin());
        user.setPassword(userDto.getPassword());
        user.setPerson(personFromDTO(userDto.getPerson()));
        user.setLocale(userDto.getLocale());
        user.setTimezone(userDto.getTimezone());
        return user;
    }

    public com.ordertracker.entity.Person personToDTO(Person person) {
        return new com.ordertracker.entity.Person(person.getId(), person.getVersion(), person.getFirstName(),person.getLastName(),
                person.getOld(),person.getContactPhone(), Sex.valueOf(person.getGender().name()));
    }

    public Person personFromDTO(com.ordertracker.entity.Person personDto) {
        Person person = new Person();
        person.setId(personDto.getId());
        person.setVersion(personDto.getVersion());
        person.setFirstName(personDto.getFirstName());
        person.setLastName(personDto.getLastName());
        person.setOld(personDto.getOld());
        person.setContactPhone(personDto.getContactPhone());
        person.setGender(SEX.valueOf(personDto.getGender().getName()));
        return person;
    }

    public Organization organizationFromDTO(com.ordertracker.entity.Organization orgDto, AuditEntity auditEntity) {
        Organization organization = new Organization(orgDto.getShortName(),orgDto.getPrefix());
        organization.setFullName(orgDto.getFullName());
        organization.setCode(orgDto.getCode());
        organization.setDescription(orgDto.getDescription());
        organization.setAuditField(auditEntity);
        return organization;
    }

    public Organization organizationFromDTO(com.ordertracker.entity.Organization orgDto) {
        Organization organization = new Organization(orgDto.getShortName(),orgDto.getPrefix());
        organization.setFullName(orgDto.getFullName());
        organization.setDescription(orgDto.getDescription());
        organization.setId(orgDto.getId());
        organization.setVersion(orgDto.getVersion());
        organization.setCode(orgDto.getCode());
        return organization;
    }

    public com.ordertracker.entity.Organization organizationToDTO(Organization organization) {
        com.ordertracker.entity.Organization orgDto = new com.ordertracker.entity.Organization();
        orgDto.setId(organization.getId());
        orgDto.setVersion(organization.getVersion());
        orgDto.setCode(organization.getCode());
        orgDto.setDescription(organization.getDescription());
        orgDto.setFullName(organization.getFullName());
        orgDto.setShortName(organization.getShortName());
        orgDto.setPrefix(organization.getPrefix());
        return orgDto;
    }

    public com.ordertracker.entity.Client clientToDTO(Client client) {
        com.ordertracker.entity.Client clientDto = new com.ordertracker.entity.Client();
        clientDto.setId(client.getId());
        clientDto.setVersion(client.getVersion());
        clientDto.setName(client.getName());
        clientDto.setCode(client.getCode());
        clientDto.setDescription(client.getDescription());
        clientDto.setOrganization(organizationToDTO(client.getOrganization()));
        return clientDto;
    }

    public Client clientFromDTO(com.ordertracker.entity.Client clientDto) {
        Client client = new Client();
        client.setCode(clientDto.getCode());
        client.setName(clientDto.getName());
        client.setDescription(clientDto.getDescription());
        client.setVersion(clientDto.getVersion());
        client.setId(clientDto.getId());
        client.setOrganization(organizationFromDTO(clientDto.getOrganization()));
        return client;
    }

    public AuditEntity generateAuditInfo(Session session, boolean updated) {
        final AuditEntity auditEntity = new AuditEntity();
        Date dateModify = DateUtils.getCurrDate();
        if (!updated) {
            auditEntity.setCreatedByUser(session.getUser());
            auditEntity.setCreatedDate(dateModify);
        }
        auditEntity.setLastUpdateByUser(session.getUser());
        auditEntity.setLastUpdate(dateModify);
        return auditEntity;
    }

    public com.ordertracker.entity.Employee employeeToDTO(Employee employee) {
        com.ordertracker.entity.Employee empDto = new com.ordertracker.entity.Employee();
        empDto.setId(employee.getId());
        empDto.setVersion(employee.getVersion());
        empDto.setCode(employee.getCode());
        empDto.setName(employee.getName());
        empDto.setOrganization(organizationToDTO(employee.getOrganization()));
        empDto.setPosition(positionToDTO(employee.getPosition()));
        empDto.setRole(roleToDTO(employee.getRole()));
        empDto.setUser(userToDTO(employee.getUser()));
        return empDto;
    }
    
    public com.ordertracker.entity.Position positionToDTO(Position position) {
        com.ordertracker.entity.Position posDto = new com.ordertracker.entity.Position();
        posDto.setId(position.getId());
        posDto.setName(position.getName());
        return posDto;
    }

    public Position positionFromDTO(com.ordertracker.entity.Position posDto) {
        Position position = new Position(posDto.getName());
        return position;
    }

    public Role roleFromDTO(com.ordertracker.entity.Role roleDto) {
        Role role = new Role(roleDto.getName(), ROLE_TYPE.valueOf(roleDto.getType().getName()));
        role.setId(roleDto.getId());
        role.setVersion(roleDto.getVersion());
        return role;
    }

    public com.ordertracker.entity.Role roleToDTO(Role role) {
        return new com.ordertracker.entity.Role(role.getId(),role.getVersion(),
                role.getName(), com.ordertracker.entity.Role.Type.valueOf(role.getRoleType().getName().toUpperCase()));
    }

}
